package com.liu.sx.controller;

import com.liu.sx.common.entity.PageResult;
import com.liu.sx.common.entity.Result;
import com.liu.sx.common.status.CodeEnum;
import com.liu.sx.entity.Voltage;
import com.liu.sx.service.VoltageService;
import com.liu.sx.util.DateTimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RequestMapping("/voltage")
@RestController
public class VoltageController {

    @Autowired
    private VoltageService voltageService;

    @GetMapping
    public Result findAll(int page,int rows,String query){
        Page<Voltage> pages =voltageService.findByQuery(page,rows,query);
        return new Result(true, CodeEnum.OK.getCode(),"查询成功",new PageResult<Voltage>(pages.getTotalElements(),pages.getContent()));
    }



    @PostMapping("/{address}")
    public List<Map<String,Object>> getVoltageFromAdress(@PathVariable String address){
        List<Map<String,Object>> list = new ArrayList<>();
        List<Voltage> voltages =voltageService.findByAdress(address);
        for(Voltage v :voltages){
           Map<String,Object> map = new HashMap<>();
           map.put("address",v.getAddress());
           map.put("rq", DateTimeUtil.DateToString(v.getRq(),"yyyyMMdd"));
           map.put("volte",v.getVolt());
           map.put("count",v.getCount());
           map.put("sbsj",DateTimeUtil.DateToString(v.getBwTime()));
           map.put("lightstate",v.getLightState());
           map.put("singn",v.getSignNum());
            list.add(map);

        }


        return list;
    }

}
